<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 粉丝
 *
 */
class Fans extends CI_Controller {
	function __construct()
	{
		parent::__construct();
		//加载usermodel
		$this->load->model('usermodel');
		$this->uid = $this->usermodel->get_id_by_token($this->input->post('token'));
		$this->load->model('fansmodel');
	}
	
	
	function index()
	{
		//默认是我的粉丝
		$this->myfans();
	}
	
	/**
	 * 我的粉丝
	 *
	 */
	function myfans()
	{
		header('Content-type:text/xml');// 向浏览器输出 xml
		//加载dblib
		/*$this->load->library('dblib');
		$this->dblib->set_query_limit();
		$this->db->select('u1.id, u1.name, u1.photo, count(app1.name) as samesofts')
				->from('user as u1')
				->join('fans as f', 'u1.id=f.fanid')
				->join('app as app1', 'app1.uid=f.fanid')
				->where('f.uid', $this->uid)
				->where("app1.name in (SELECT app2.name FROM app AS app2 JOIN user AS u2 ON u2.id=app2.uid WHERE u2.id='$this->uid')")
				->group_by('u1.id')
				->order_by('samesofts', 'desc');*/
		
		$fans = $this->fansmodel->get_fans($this->uid);
		

		if(count($fans) > 0){
			$this->load->model('attentionsmodel');
			foreach ($fans as $k=>$fan) {
				if($this->attentionsmodel->check_attention($this->uid, $fan['uid'])){
					//当前用户关注过此用户
					$fans[$k]['have_attention'] = 1;
				}else{
					$fans[$k]['have_attention'] = 0;
				}
				//查找相同应用数目
				$this->db->select('count(app1.name) as samesofts')
						 ->from('app as app1')
						 ->where('app1.uid', $fan['uid'])
						 ->where("app1.name in (SELECT app2.name FROM app AS app2 JOIN user AS u2 ON u2.id=app2.uid WHERE u2.id='$this->uid')")
						 ->group_by('app1.uid');
				$query = $this->db->get();
				$samesofts = $query->row_array();
				$samesofts = count($samesofts) > 0 ? $samesofts['samesofts'] : 0;
				$fans[$k]['samesofts'] = $samesofts;
			}
			//查询有多少粉丝
			$this->db->select('count(*) as count')
						->from('fans')
						->where('uid', $this->uid);
			$query = $this->db->get();
			$row = $query->row_array();
			$fans_count = $row['count'];//粉丝人数
			
			$this->xmllib->append_xml('fans_count', $fans_count);

			//输出最终的xml
			echo $this->xmllib->get_xml($fans, array('fans'), 'myfans');
			exit;
		}else{
			//没有粉丝
			echo $this->xmllib->empty_xml();
			exit;
		}
	}
	
	/**
	 * 查看别人的粉丝
	 *
	 */
	function other_myfans()
	{
		header('Content-type:text/xml');// 向浏览器输出 xml
		$uid = $this->input->post('uid');
		$fans = $this->fansmodel->get_fans($uid);
		if(count($fans) > 0){
			$this->load->model('attentionsmodel');
			foreach ($fans as $k=>$fan) {
				if($this->attentionsmodel->check_attention($this->uid, $fan['uid'])){
					//当前用户关注过此用户
					$fans[$k]['have_attention'] = 1;
				}else{
					$fans[$k]['have_attention'] = 0;
				}
			}
			//查询有多少粉丝
			$this->db->select('count(*) as count')
						->from('fans')
						->where('uid', $uid);
			$query = $this->db->get();
			$row = $query->row_array();
			$fans_count = $row['count'];//粉丝人数
			
			$this->xmllib->append_xml('fans_count', $fans_count);
			//输出最终的xml
			echo $this->xmllib->get_xml($fans, array('fans'), 'myfans');
			exit;
		}else{
			//没有粉丝
			echo $this->xmllib->empty_xml();
			exit;
		}
	}

	/**
	 * 成为xxx的粉丝
	 *
	 * @param unknown_type $uid
	 */
	function addtofans($uid='')
	{
		//fanid = $this->uid;
		header('Content-type:text/xml');// 向浏览器输出 xml
		if (empty($uid)) {
			//非法操作
			echo $this->xmllib->error_xml('要成为其粉丝的用户无效');
			exit;
		}
		
		if($uid == $this->uid) {
			//非法操作
			echo $this->xmllib->error_xml('不能成为自己的粉丝');
			exit;
		}
		//查询要关注的用户是否存在
		$this->load->model('usermodel');
		if(!$this->usermodel->check_user_by_id($uid)){
			//不存在
			echo $this->xmllib->error_xml('要成为其粉丝的用户不存在');
			exit;
		}
		
		//查看之前当前用户是不是其粉丝
		if($this->fansmodel->check_fans($uid, $this->uid)){
			//已经是粉丝关系了，不要重复此操作
			echo $this->xmllib->error_xml('您已经是其粉丝，请不要重复此操作');
			exit;
		}
		
		//执行操作，成为粉丝
		if($this->fansmodel->insert_fans($uid, $this->uid)){
			//成为粉丝成功
			echo $this->xmllib->success_xml('成为粉丝成功');
			exit;
		}else{
			//成为粉丝失败
			echo $this->xmllib->error_xml('成为粉丝失败，请稍后再试');
			exit;
		}
	}
	
}

/* End of file fans.php */
/* Location: ./application/controllers/fans.php */